import {fetch} from '@/utils/fetch'
import {task, progress, group, user, document, pageInfo, groupMember} from '../fragment'

export const getAllByDid = async (documentId) => {
  let res = await fetch({
    query: `{
      documentTasks (documentId: ${documentId}, pageIndex: 0, pageSize: 30) {
        rows {
          ${task}
          user {
            ${user}
          }
          group {
            ${group}
          }
        }
      }
    }`,
    cache: false
  })
  return res.data.documentTasks.rows
}

export const getAllByIds = async (taskIds) => {
  let res = await fetch({
    query: `query ($taskIds: [Int!]!) {
      taskGetAllByIds (taskIds: $taskIds) {
        ${task}
        group {
          ${group}
        }
        document {
          ${document}
        }
      }
    }`,
    variables: {
      taskIds
    },
    cache: false
  })
  return res.data.taskGetAllByIds
}

export const getMyCreatesByPage = async ({type = 'published', params = null, currentPage = 1, pageSize = 20, cache = true}) => {
  let res = await fetch({
    query: `query ($type: String, $params: JSON, $currentPage: Int, $pageSize: Int) {
      taskGetMyCreates (filter: {type: $type, params: $params}, currentPage: $currentPage, pageSize: $pageSize) {
        page {
          ${pageInfo}
        }
        rows {
          ${task}
          document {
            ${document}
          }
          group {
            ${group}
          }
        }
      }
    }`,
    cache,
    variables: {
      type,
      params,
      currentPage,
      pageSize
    }
  })
  return res.data.taskGetMyCreates
}

export const getById = async (taskId) => {
  let res = await fetch({
    query: `{
      task (id: ${taskId}) {
        ${task}
        document {
          ${document}
          user {
            ${user}
          }
        }
        group {
          ${group}
        }
        creator {
          ${user}
        }
        progresses {
          ${progress}
          user {
            ${user}
          }
        }
      }
    }`,
    cache: false
  })
  return res.data.task
}

export const urge = async (taskId) => {
  let res = await fetch({
    query: `mutation {
      urgeTask (taskId: ${taskId}) {
        ${user}
      }
    }`,
  })
  return res.data.urgeTask
}

export const modify = async ({taskId, endAt = '', prePublishAt = ''}) => {
  let res = await fetch({
    query: `mutation ($taskId: Int!, $endAt: String, $prePublishAt: String) {
      taskModify (taskId: $taskId, endAt: $endAt, prePublishAt: $prePublishAt) {
        ${task}
      }
    }`,
    variables: {
      taskId: parseInt(taskId),
      endAt,
      prePublishAt
    }
  })
  return res.data.urgeTask
}

export const getByPage = async ({type, currentPage = 1}) => {
  let res = await fetch({
    query: `query (
      $type: String,
      $params: JSON,
      $currentPage: Int,
      $pageSize: Int
    ) {
      taskGetMyCreates (
        filter: {
          type: $type,
          params: $params
        },
        currentPage: $currentPage,
        pageSize: $pageSize
      ) {
        page {
          ${pageInfo}
        }
        rows {
          ${task}
          document {
            ${document}
          }
          group {
            ${group}
          }
        }
      }
    }`,
    variables: {
      type,
      currentPage
    }
  })
  return res.data.taskGetMyCreates
}

export const getTotalUnreadCount = async () => {
  let res = await fetch({
    query: `{
      taskGetMyUnreadCount
    }`,
  })
  return res.data.taskGetMyUnreadCount
}
